use semver::Version;

use common_base::once_cell::sync::Lazy;

pub static IOSP_COMMIT_VERSION: Lazy<String> = Lazy::new(|| {
    let git_tag = option_env!("VERGEN_GIT_SEMVER");
    let git_sha = option_env!("VERGEN_GIT_SHA_SHORT");
    let rustc_semver = option_env!("VERGEN_RUSTC_SEMVER");
    let timestamp = option_env!("VERGEN_BUILD_TIMESTAMP");

    match (git_tag, git_sha, rustc_semver, timestamp) {
        (Some(v1), Some(v2), Some(v3), Some(v4)) => format!("{}-{}(rust-{}-{})", v1, v2, v3, v4),
        _ => String::new(),
    }
});

pub static DEVELOP_SEMVER: Lazy<Version> = Lazy::new(|| {
    //
    let build_semver = option_env!("VERGEN_GIT_SEMVER");
    let semver = build_semver.expect("VERGEN_GIT_SEMVER can not be None");

    let semver = semver.strip_prefix('v').unwrap_or(semver);

    Version::parse(semver).unwrap_or_else(|e| panic!("Invalid semver: {:?}: {}", semver, e))
});
